package com.github.niefy.modules.third.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.niefy.common.annotation.AccessLimit;
import com.github.niefy.common.utils.HttpClientUtil;
import com.github.niefy.common.utils.R;
import com.github.niefy.modules.third.dto.in.ThirdSpmIn_001;
import com.github.niefy.modules.third.dto.in.ThirdSpmIn_002;
import com.github.niefy.modules.third.dto.out.ThirdSpmOut_001;
import com.github.niefy.modules.third.dto.out.ThirdSpmOut_002;
import com.github.niefy.modules.third.enums.SecApiURLEnum;
import com.github.niefy.modules.third.enums.SpmApiidEnum;
import com.github.niefy.modules.third.vo.StuSpmListVo;
import com.github.niefy.modules.third.vo.StuSpmScoreListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

/**
 * spm查询转发接口
 */
@RestController
@RequestMapping("/third/spm")
@Tag(name = "spm查询转发接口")
@RequiredArgsConstructor
@Slf4j
public class ThirdSpmController {

    @Value("${sec-api.spm.domain}")
    String spmDomain;
    @Value("${sec-api.spm.accesskey}")
    String accesskey;
    private final WxMpService wxMpService;


    @GetMapping("/stuSpmList")
    @ResponseBody
    @AccessLimit(seconds = 20, maxCount = 2)
    @Operation(summary = "查询学生学业进展信息",description = "查询学生学业进展信息，对应学业状况菜单")
    public R getStuSpmList(HttpServletRequest request, HttpServletResponse response,
                         @RequestAttribute String thirdId) throws WxErrorException {
        String url = spmDomain + SecApiURLEnum.SPM_CALL;
        var thirdSpmOut001 = ThirdSpmOut_001.builder()
                .studentno(thirdId)
                .build();
        thirdSpmOut001.createSign(SpmApiidEnum.querystutermspmlist.name(), "SPMSTU", "studentno",
                accesskey, thirdId);
        var thirdSpmIn_001 = HttpClientUtil.http2GetJsonSync(url, ThirdSpmIn_001.class, thirdSpmOut001);
        var stuSpmListVo = BeanUtil.copyProperties(thirdSpmIn_001, StuSpmListVo.class);
        //只取最新一条返回前台，若需要查看往期信息可注掉
        stuSpmListVo.setSpmlist(stuSpmListVo.getSpmlist().subList(0,1));
        return R.ok().put("stuSpmList",stuSpmListVo);
    }


    @GetMapping("/stuSpmScoreList")
    @ResponseBody
    @Operation(summary = "查询学生学业进展成绩信息",description = "查询学生学业进展信息，对应成绩")
    public R getStuSpmScoreList(HttpServletRequest request, HttpServletResponse response,
                           @RequestAttribute String thirdId) throws WxErrorException {
        String url = spmDomain + SecApiURLEnum.SPM_CALL;
        var thirdSpmOut002 = ThirdSpmOut_002.builder()
                .studentno(thirdId)
                .build();
        thirdSpmOut002.createSign(SpmApiidEnum.querystuspmscorelist.name(), "SPMSTU", "studentno",
                accesskey, thirdId);
        var thirdSpmIn_002 = HttpClientUtil.http2GetJsonSync(url, ThirdSpmIn_002.class, thirdSpmOut002);
        var stuSpmScoreListVo = new StuSpmScoreListVo();
        //var stuSpmListVo = BeanUtil.copyProperties(thirdSpmIn_002, StuSpmListVo.class);
        //根据学年学期分组
        var collect = thirdSpmIn_002.getSpmscorelist().stream().collect(
                Collectors.groupingBy(ThirdSpmIn_002.spmscore::getAcademicyearno,
                        Collectors.groupingBy(ThirdSpmIn_002.spmscore::getTermno)));
        collect.forEach((academicyearno,mapGroupByacademicyearno)->{
            mapGroupByacademicyearno.forEach((termno,mapGroupBytermno)->{
                var spmScoreVo = stuSpmScoreListVo.new SpmScoreVo();
                mapGroupBytermno.forEach(spmscore -> {
                    BeanUtil.copyProperties(spmscore,spmScoreVo);
                    var courseScoreVo = stuSpmScoreListVo.new CourseScoreVo();
                    BeanUtil.copyProperties(spmscore, courseScoreVo);
                    spmScoreVo.getCourseScoreList().add(courseScoreVo);
                });
                stuSpmScoreListVo.getSpmScoreList().add(spmScoreVo);
            });
        });
        return R.ok().put("stuSpmScoreList", stuSpmScoreListVo);
    }
}
